home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / speech / examples / recognize_c.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.9 KB  |  163 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include    <stdio.h>
  18. #include    <assert.h>
  19. #include    <X11/Xlib.h>
  20. #include    <X11/Xresource.h>
  21. #include    <speech/Recognizer.h>
  22. #include    <speech/Word.h>
  23. #include    <speech/WordCallbackBindings.h>
  24. #include    <speech/Vocabulary.h>
  25. #include    <speech/Condition.h>
  26. #include    <speech/Event.h>
  27. #include    <speech/Error.h>
  28. #include    <speech/Shorthand.h>        /* required for C API */
  29.  
  30. void doThis( const Event* event, void* v )
  31.     {
  32.     fprintf( stderr, "do this called with %d\n", Event_type( event )) ;
  33.     fflush( stderr ) ;
  34.     }
  35. void doThat( const Event* event, void* v )
  36.     {
  37.     fprintf( stderr, "do this called with %d\n", Event_type( event )) ;
  38.     fflush( stderr ) ;
  39.     }
  40. void doTheOther( const Event* event, void* v )
  41.     {
  42.     fprintf( stderr, "do this called with %d\n", Event_type( event )) ;
  43.     fflush( stderr ) ;
  44.     }
  45.  
  46. void usage( const char* const progName )
  47.     { fprintf( stderr, "usage <word1> <word2>\n" ) ; fflush( stderr ) ; }
  48.  
  49. int main( int argc, char* argv[] )
  50. {
  51.     XEvent event ;
  52.  
  53.     Window window ;
  54.  
  55.     Vocabulary* localWords ;
  56.     Vocabulary* globalWords ;
  57.  
  58.     unsigned char swapConditionsUsingVocabularies = 0 ;
  59.  
  60.     char* applicationClass = "Recognize" ;
  61.     char* applicationName = argv[0] ;
  62.  
  63.     char fileName[256] ;
  64.     XrmDatabase xrmDatabase    ;
  65.  
  66.     Recognizer* recognizer ;
  67.  
  68.     sprintf( fileName, "%s/.Xdefaults", getenv( "HOME" ) ) ;
  69.     xrmDatabase    = XrmGetFileDatabase( fileName ) ; /* toolkit-dependent */
  70.  
  71.     recognizer = Recognizer_new(
  72.             applicationClass, applicationName, xrmDatabase,
  73.             "",     /* default display, program name */
  74.             Recognizer_RecognitionInterest()     /* recognition events */
  75.                     | Recognizer_RejectionInterest()    /* and all others */
  76.                     | Recognizer_AmbiguousInterest()
  77.                     | Recognizer_ErrorInterest() ) ;
  78.     assert( Recognizer_status( recognizer ) == Error_OK ) ;
  79.  
  80.     /* words with global focus */
  81.     {
  82.     ActionCallbackBinding globalActionFunctionBindings[] = {
  83.         "doThis", doThis, (void*)0,
  84.         "doThat", doThat, (void*)0,
  85.         (char*)0, (CallbackFunctionPointer)0, (void*)0,
  86.         } ;
  87.  
  88.     Condition* globalCondition = Condition_newGlobal() ;
  89.     Recognizer_newWords( recognizer, globalActionFunctionBindings,
  90.             globalCondition, &globalWords ) ;
  91.     if( Recognizer_status( recognizer ) != Error_OK )
  92.         { fprintf( stderr, "trouble loading global words\n" ) ; fflush( stderr ) ; }
  93.     Condition_delete( globalCondition ) ;
  94.     }
  95.  
  96.  
  97.     /* words with window focus */
  98.     {
  99.     ActionCallbackBinding localActionFunctionBindings[] = {
  100.         "doTheOther", doTheOther, (void*)0,
  101.         (char*)0, (CallbackFunctionPointer)0, (void*)0,
  102.         } ;
  103.  
  104.     /* toolkit dependent */
  105.     void* v = "client data" ;
  106.     int screen = DefaultScreen( Recognizer_dpy( recognizer ) ) ;
  107.     Window root = RootWindow( Recognizer_dpy( recognizer ), screen ) ;
  108.     long white = WhitePixel( Recognizer_dpy( recognizer ), screen ) ;
  109.     long black = BlackPixel( Recognizer_dpy( recognizer ), screen ) ;
  110.     window = XCreateSimpleWindow( Recognizer_dpy( recognizer ), root,
  111.             100, 100, 100, 100, 1, white, black ) ;
  112.     XMapWindow( Recognizer_dpy( recognizer ), window ) ;
  113.  
  114.     {
  115.     Condition* localCondition = Condition_newWindow( window ) ;
  116.     Recognizer_newWords( recognizer, localActionFunctionBindings,
  117.             localCondition, &localWords ) ;
  118.     if( Recognizer_status( recognizer ) != Error_OK )
  119.         { fprintf( stderr, "trouble loading local words\n" ) ; fflush( stderr ) ; }
  120.     Condition_delete( localCondition ) ;
  121.     }
  122.     }
  123.  
  124.     /* vocabulary usage */
  125.     if( swapConditionsUsingVocabularies )
  126.         {
  127.         Condition* globalCondition = Condition_newGlobal() ;
  128.         Condition* localCondition = Condition_newWindow( window ) ;
  129.  
  130.         Vocabulary_enable( localWords, globalCondition ) ;
  131.         Vocabulary_enable( globalWords, localCondition ) ;
  132.  
  133.         Condition_delete( globalCondition ) ;
  134.         Condition_delete( localCondition ) ;
  135.         }
  136.  
  137.     Vocabulary_delete( globalWords ) ;
  138.     Vocabulary_delete( localWords ) ;
  139.  
  140.     /* toolkit-dependent modification here */
  141.     do
  142.         {
  143.         XNextEvent( Recognizer_dpy( recognizer ), &event ) ;
  144.  
  145.         fprintf( stdout, "event type = %d\n", event.type ) ;
  146.         fflush( stdout ) ;
  147.  
  148.         if( Recognizer_isSpeechEvent( recognizer, &event ) )
  149.             Recognizer_processEvent( recognizer, &event ) ;
  150.          else
  151.             ; /* process as other/normal event */
  152.  
  153.         } while( event.type != DestroyNotify ) ;    /* just to get rid of warning */
  154.  
  155.     XrmDestroyDatabase( xrmDatabase ) ;
  156.  
  157.     {
  158.     Error retVal = Recognizer_status( recognizer ) ;
  159.     Recognizer_delete( recognizer ) ;
  160.     return retVal ;
  161.     }
  162.     }
  163.